package com.spring.http;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;
import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.handler.codec.http.multipart.Attribute;
import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
import io.netty.handler.codec.http.multipart.InterfaceHttpData;

public class HttpUtils {
	/**
	 * 将http请求的参数，解析成map
	 * 
	 * @param fullReq
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> getHttpParams(FullHttpRequest fullReq) throws Exception {
		HttpMethod method = fullReq.getMethod();

		final Map<String, Object> parmMap = new HashMap<String, Object>();
		if (HttpMethod.GET == method) {
			// 是GET请求
			QueryStringDecoder decoder = new QueryStringDecoder(fullReq.getUri());
			decoder.parameters().entrySet().forEach(entry -> {
				// entry.getValue()是一个List, 只取第一个元素
				parmMap.put(entry.getKey(), entry.getValue().get(0));
			});
		} else if (HttpMethod.POST == method) {
			if (fullReq.getUri().contains("?")) {
				// GET
				QueryStringDecoder decoder = new QueryStringDecoder(fullReq.getUri());
				decoder.parameters().entrySet().forEach(entry -> {
					// entry.getValue()
					parmMap.put(entry.getKey(), entry.getValue().get(0));
				});
			}
			// 是POST请求
			HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(fullReq);
			decoder.offer(fullReq);
			List<InterfaceHttpData> parmList = decoder.getBodyHttpDatas();
			for (InterfaceHttpData parm : parmList) {
				Attribute data = (Attribute) parm;
				parmMap.put(data.getName(), data.getValue());
			}

		} else {
			// 不支持其它方法
			throw new Exception("解析参数出错"); // 这是个自定义的异常, 可删掉这一行
		}

		return parmMap;
	}

	/**
	 * 返回响应
	 * 
	 * @return
	 */
	public static FullHttpResponse setRes(ReturnData data) {
		FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK,Unpooled.wrappedBuffer(JSONObject.toJSONString(data).getBytes()));
		response.headers().set(CONTENT_TYPE, "text/plain;charset=utf-8");
		response.headers().set(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
		response.headers().set(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_METHODS, "GET,POST");
		response.headers().set(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS,
				"authorization,origin,X-Requested-With, Content-Type, Accept");
		response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
		return response;
	}

	public static FullHttpResponse errorSystem(ReturnData data) throws UnsupportedEncodingException {
		return setRes(data);
	}

}
